import 'package:file_transfer/manager/webrtc_manager.dart';
import 'package:file_transfer/pojo/channel_message.dart';
import 'package:file_transfer/pojo/message.dart';
import 'package:flutter/widgets.dart';

class MessageModel with ChangeNotifier{

  // 在线的客户端
  List<User> _onlineUserList = [];
  List<User> get onlineUserList => _onlineUserList;
  set onlineUserList(List<User> value){
    _onlineUserList.clear();

    // 如果只有一个客户端就直接连接
    if(value.length == 1){
      client = value.first;
    }else if(value.isEmpty){
      client = User();
    }

    _onlineUserList = value;
    notifyListeners();
  }

  // 选择发送的客户端
  User _client = User();
  User get client => _client;
  set client(User value){
    _client = value;
    _selectAllClient = false;
    notifyListeners();
  }

  // 是否为发送全部在线客户端
  bool _selectAllClient = false;
  bool get selectAllClient => _selectAllClient;
  set selectAllClient(bool value){
    _selectAllClient = value;
    notifyListeners();
  }

  // key是否为当前在线列表的用户
  bool isRightKey(){
    String key = _client.key;
    for (String value in _onlineUserList.map((e) => e.key)) {
      if(key == value){
        return true;
      }
    }
    return false;
  }

  // 接收的的消息
  String _receiveMessage = "";
  String get receiveMessage => _receiveMessage;
  set receiveMessage(String value){
    _receiveMessage = value;
    notifyListeners();
  }

  // 聊天消息列表
  List<ChannelMessage> _chatMessageList = [];
  List<ChannelMessage> get chatMessageList => _chatMessageList;
  addChatMessage(ChannelMessage message){
    _chatMessageList.add(message);
    notifyListeners();
    // 跳转交给布局完成回调
  }

  // 消息列表控制器
  final ScrollController channelMessageController = ScrollController();
  // 消息列表到底部方法回调
  double _lastPosition = 0;
  channelMessageToBottom({required VoidCallback toBottom, required VoidCallback downSlide}){
    channelMessageController.addListener(() {
      if(
      channelMessageController.position.pixels ==
          channelMessageController.position.maxScrollExtent){
        toBottom();
      }else{
        double currentPosition = channelMessageController.position.pixels;
        if(_lastPosition - currentPosition > 10 || currentPosition ==
        channelMessageController.position.minScrollExtent){
          downSlide();
        }
        _lastPosition = currentPosition;
      }

    });
  }

  // 接受的文件进度
  int _receiveFileProgress = 0;
  int get receiveFileProgress => _receiveFileProgress;
  set receiveFileProgress(int value){
    _receiveFileProgress = value;
    notifyListeners();
  }

  // 当前接接收的文件
  FileChannelMessage _receivingFile = FileChannelMessage("file", 0, local: true);
  FileChannelMessage get receivingFile => _receivingFile;
  set receivingFile(FileChannelMessage value){
    _receivingFile = value;
    notifyListeners();
  }

  // 发送的文件进度
  int _sendFileProgress = 0;
  int get sendFileProgress => _sendFileProgress;
  set sendFileProgress(int value){
    _sendFileProgress = value;
    // sendFileProgressRatio = (_sendFileProgress/_sendingFile.size).toStringAsFixed(2)*100;
    notifyListeners();
  }
  // String sendFileProgressRatio = "0";

  // 当前发送的文件
  FileChannelMessage _sendingFile = FileChannelMessage("file", 0, local: true);
  FileChannelMessage get sendingFile => _sendingFile;
  set sendingFile(FileChannelMessage value){
    _sendingFile = value;
    notifyListeners();
  }

  // 是否可以进行下一轮文件发送
  bool isSendNextFile = true;

}